package com.job.DQH.dqh715.inClass;

import java.util.Scanner;

/**
 * 方法一：用反转方法
 * 方法二：将第i个字符与第length-i-1个字符比较是否相等
 */
public class homework_isPalindrome {
    public static void main(String[] args) {
        homework_isPalindrome x = new homework_isPalindrome();
        //判断字符串是否为各位数和为N的回文数
        Scanner in = new Scanner(System.in);

        System.out.println("请输入回文数各位数的的和");
        int In = in.nextInt();
        int sum = 0;
        int count = 0;
        String num;

        System.out.println("满足条件的回文数有：");
        for (int i = 10000; i <999999 ; i++) {
            num = i + "";
            if(x.isSum(num,In)&&x.isPalindrome(num)){
                sum += i;
                count++;
                System.out.println(i);
            }
        }

        System.out.println("所有的五位数与六位数中，各位数和为"+In+"的回文数共有"+count+"个，它们的和是"+sum);
    }

    public boolean isPalindrome(String num){

        int intnum = Integer.parseInt(num);

        for (int i = 0; i <num.length(); i++) {
            if(num.charAt(i)!=num.charAt(num.length()-i-1)){
                return false;
            }
        }
        return true;
    }

    public boolean isSum(String num,int sum){
        int temp = 0;
        int intnum = Integer.parseInt(num);
        for (int i = 0; i <num.length() ; i++) {
            temp = temp + intnum%10;
            intnum = intnum/10;
        }
        if(temp != sum)
            return false;
        return true;
    }
}
